package com.mcb_wxc.util;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @category 工具类 - 字符串实用方法类
 */
public class StringUtil {
	private static long[] ls = new long[3000];
    private static int li = 0;
	/**
	 * @category 把对象转换成String
	 */
	public final static String toString(Object obj) {
		return obj == null ? "" : obj.toString();
	}

	/**
	 * @category 调试方法
	 */
	public final static void debug(Object obj) {
		System.out.println("[DEBUG]:" + toString(obj));
	}

	/**
	 * @category 调试方法
	 */
	public final static void debug(Object[] obj) {
		for (Object tmp : obj)
			debug(tmp);
	}

	/**
	 * @category 过滤将要写入到XML文件中的字符串，即过滤掉<![CDATA[和]]>标签
	 */
	public static String toXMLFilter(Object obj) {
		if (trim(obj).equals(""))
			return " ";
		return trim(obj).replaceAll("<!\\[CDATA\\[", "&lt;!CDATA").replaceAll(
				"\\]\\]>", "]] >");
	}

	/**
	 * @category 返回一个对象的字符串，多数是处理字符串的
	 */
	public static String trim(Object obj) {
		return obj == null ? "" : String.valueOf(obj).trim();
	}

	/**
	 * @category 对一字符串数组进行去空格操作
	 */
	public final static String[] trim(String[] aStr) {
		if (aStr == null)
			return null;
		for (int i = 0; i < aStr.length; i++) {
			aStr[i] = trim(aStr[i]);
		}
		return aStr;
	}

	/**
	 * @category 过滤设置到SQL语句中的字符串
	 */
	public final static String toDBFilter(String aStr) {
		return trim(aStr).replaceAll("\\\'", "''");
	}

	/**
	 * @category 数字字符串的整型转换
	 */
	public final static int parseInt(String str, int defaultVal) {
		try {
			return Integer.parseInt(str);
		} catch (NumberFormatException ex) {
			return defaultVal;
		}
	}

	/**
	 * @category 数字字符串的整型转换
	 */
	public final static Integer parseInteger(String str, int defaultVal) {
		try {
			return Integer.valueOf(str);
		} catch (NumberFormatException ex) {
			return new Integer(defaultVal);
		}
	}

	/**
	 * @category 字符串转换成double
	 */
	public final static double parseDouble(String str, double defauleVal) {

		try {
			return Double.valueOf(str);
		} catch (NumberFormatException ex) {
			return defauleVal;
		}
	}

	/**
	 * @category 字符串转换成double
	 */
	public final static Double parseDoubleObj(String str, double defauleVal) {
		try {
			return Double.valueOf(str);
		} catch (NumberFormatException ex) {
			return new Double(defauleVal);
		}
	}

	/**
	 * @category 数字字符串的长整型转换
	 */
	public final static Long parseLong(String str) {
		// 初始化默认值为0
		return parseLongDefaultVal(str, 0);
	}

	/**
	 * @category 数字字符串的长整型转换
	 */
	public final static Long parseLong(Object o) {
		// 初始化默认值为0
		if (o == null) {
			return (long) 0;
		}
		return parseLongDefaultVal(o.toString(), 0);
	}

	/**
	 * @category 数字字符串的长整型转换
	 */
	public final static Long parseLongDefaultVal(String str, long defaultVal) {
		try {
			return Long.valueOf(str);
		} catch (NumberFormatException ex) {
			return new Long(defaultVal);
		}
	}

	/**
	 * @category 数字字符串数组转化为长整型数组
	 */
	public final static Long[] parseLong(String[] str) {
		if (str == null || str.length < 1)
			return new Long[0];
		Long[] result = new Long[str.length];
		for (int i = 0; i < str.length; i++) {
			result[i] = parseLong(str[i]);
		}
		return result;
	}

	/**
	 * @category 数字字符串的整型转换
	 */
	public final static Integer[] parseInteger(String[] str, int defaultVal) {
		if (str == null || str.length < 1)
			return new Integer[0];
		Integer[] result = new Integer[str.length];
		for (int i = 0; i < str.length; i++) {
			result[i] = parseInteger(str[i], defaultVal);
		}
		return result;
	}

	/**
	 * @category 数字字符串的整型转换
	 */
	public final static int[] parseInt(String[] str, int defaultVal) {
		if (str == null || str.length < 1)
			return new int[0];
		int[] result = new int[str.length];
		for (int i = 0; i < str.length; i++) {
			result[i] = parseInt(str[i], defaultVal);
		}
		return result;
	}

	/**
	 * @category 将指定的值从源数组中进行排除，并返回一个新数组
	 */
	public final static int[] exclude(int[] src, int[] target) {
		if (target == null || target.length < 1)
			return src;
		StringBuilder tmp = new StringBuilder();
		for (int tt : src) {
			if (!include(target, tt))
				tmp.append(tt + ",");
		}
		if (tmp.length() > 1 && tmp.charAt(tmp.length() - 1) == ',') {
			tmp.deleteCharAt(tmp.length() - 1);
		}
		if (tmp.toString().trim().length() < 1)
			return new int[0];
		String[] array = tmp.toString().split(",");
		return parseInt(array, 0);
	}

	/**
	 * @category 将指定的target数组从src源数组中进行排除.
	 */
	public final static String[] exclude(String[] src, String[] target) {
		if (target == null || target.length < 1)
			return src;
		StringBuilder tmp = new StringBuilder();
		for (String str : src) {
			if (!include(target, str))
				tmp.append(str + ",");
		}
		if (tmp.length() > 1 && tmp.charAt(tmp.length() - 1) == ',') {
			tmp.deleteCharAt(tmp.length() - 1);
		}
		if (tmp.toString().trim().length() < 1)
			return new String[0];
		return tmp.toString().split(",");
	}

	/**
	 * @category 将指定的数组字符串使用指定的符号进行连接
	 */
	public final static String join(Object[] src, String spliter) {
		if (src == null || src.length < 1)
			return "";
		StringBuffer tmp = new StringBuffer();
		// String mySpliter = trim(spliter).intern() == "" ? "," : spliter;
		String mySpliter = trim(spliter);
		for (int i = 0; i < src.length; i++) {
			tmp.append(src[i]);
			if (i < src.length - 1)
				tmp.append(mySpliter);
		}
		return tmp.toString();
	}

	/**
	 * @category 将指定的数组字符串使用指定的符号进行连接.
	 */
	public final static String join(int[] src, String spliter) {
		if (src == null || src.length < 1)
			return "";
		StringBuffer tmp = new StringBuffer();
		String mySpliter = trim(spliter).intern() == "" ? "," : spliter;
		for (int i = 0; i < src.length; i++) {
			tmp.append(src[i] + mySpliter);
		}
		return tmp.deleteCharAt(tmp.length() - 1).toString();
	}

	/**
	 * @category 将指定的字符串数组使用指定的字符串进行保围，比如一字符串数组如下： ["hello",
	 *           "world"],使用的包围字符串为"'",那么返回的结果就应该是： ["'hello'","'world'"].
	 */
	public final static String[] arround(String[] src, String str) {
		if (src == null || src.length < 1)
			return src;
		String[] result = new String[src.length];
		for (int i = 0; i < src.length; i++) {
			result[i] = str + src[i] + str;
		}
		return result;
	}

	/**
	 * @category 判断指定的字符串是否是空指针或空串
	 */
	public final static boolean isNullAndBlank(String src) {
		return trim(src).intern() == "";
	}

	/**
	 * @category 将指定的字符串转换成符合JavaBean规范的方法名称！ 此方法将只转换第一个字母为大写字母，比如有一字符串是
	 *           helloWorld,那么转换后就是：setHelloWorld.另外如果给出
	 *           的字符串为空（null或""），那么将直接返回空字符串！
	 */
	public final static String toMethodName(String name) {
		String tmp = trim(name).intern();
		if (tmp == "")
			return "";
		if (tmp.length() < 2) {
			return "set" + name.toUpperCase();
		} else {
			return "set" + name.substring(0, 1).toUpperCase()
					+ name.substring(1);
		}
	}

	/**
	 * @category 将map中的键和值进行对应并返回成字符串.
	 */

	public final static String mapToString(Map<?, ?> map) {
		if (map == null)
			return null;
		StringBuilder buf = new StringBuilder("[");
		Iterator<?> it = map.keySet().iterator();
		while (it.hasNext()) {
			Object key = it.next();
			buf.append(String.valueOf(key) + ":" + String.valueOf(map.get(key))
					+ ",");
		}
		if (buf.charAt(buf.length() - 1) == ',') {
			buf.deleteCharAt(buf.length() - 1);
		}
		return buf.append(']').toString();
	}

	/**
	 * @category 检查给定的数组长度是否一致，若全部一致则返回true，否则返回false
	 */
	public final static boolean sameLength(String[]... array) {
		if (array.length <= 1)
			return true;
		for (int i = 0; i < array.length; i++) {
			String[] str1 = array[i];
			for (int j = i + 1; j < array.length; j++) {
				String[] str2 = array[j];
				if (str1 == null && str2 == null)
					continue;
				if (str1 == null && str2 != null)
					return false;
				if (str1 != null && str2 == null)
					return false;
				if (str1.length != str2.length)
					return false;
			}
		}
		return true;
	}

	/**
	 * @category 检查指定的数组中是否包含了指定的数字.
	 */
	public final static boolean include(int[] source, int test) {
		if (source == null || source.length < 1)
			return false;
		for (int tmp : source) {
			if (tmp == test)
				return true;
		}
		return false;
	}

	/**
	 * @category 检查指定的字符串数组中是否包含了指定的字符串.
	 */
	public final static boolean include(String[] source, String test) {
		if (source == null || source.length < 1)
			return false;
		for (String tmp : source) {
			if (tmp == null && test == null)
				return true;
			if (tmp != null && tmp.equals(test))
				return true;
		}
		return false;
	}

	/**
	 * @category 检查指定的字符串数组中是否包含了指定的字符串，不区分大小写.
	 */
	public final static boolean includeIgnoreCase(String[] source, String test) {
		if (source == null || source.length < 1)
			return false;
		for (String tmp : source) {
			if (tmp == null && test == null)
				return true;
			if (tmp != null && tmp.equalsIgnoreCase(test))
				return true;
		}
		return false;
	}

	/**
	 * @category 检查指定的字符串数组source1是否包含另外一个字符串数组source2，不区分大小写.
	 */
	public final static boolean includeIgnoreCaseStrings(String[] source1,
			String[] source2) {
		if (source1 == null || source1.length < 1)
			return false;
		if (source2 == null || source2.length < 1)
			return false;

		for (String tmp2 : source2) {
			boolean isFind1 = false; // 记录2中的某一个是否存在1中
			for (String tm1 : source1) {
				if (tm1.equals(tmp2)) {
					isFind1 = true;
					break;
				}
			}
			if (!isFind1) {
				return false;
			}
		}
		return true;
	}

	/**
	 * @category 将指定字符串的首字母变成大写.
	 */
	public final static String capitalize(String str) {
		if (str == null || str.length() < 1)
			return str;
		if (str.length() == 1)
			return str.toUpperCase();
		return str.substring(0, 1).toUpperCase() + str.substring(1);
	}

	/**
	 * @category 将指定字符串的首字母变成小写.
	 */
	public final static String unCapitalize(String str) {
		if (str == null || str.length() < 1)
			return str;
		if (str.length() == 1)
			return str.toLowerCase();
		return str.substring(0, 1).toLowerCase() + str.substring(1);
	}

	/**
	 * @category 获取当前日期形成的路径字符串.
	 */
	public final static String getDailyDirectory() {
		return DateFormatUtil.CurrentTimeMillis("yyyy/mm/dd");
	}

	/**
	 * @category 反转字符串.
	 */
	public final static String reverse(String str) {
		if (trim(str).equals(""))
			return str;
		return new StringBuilder(str).reverse().toString();
	}

	/**
	 * @category 格式化字符串为 4 的倍数，不够就在前面添 0 同时将 后面 小数点去掉
	 */
	public final static String formatString(String str) {

		// int r = str.indexOf(".");
		if (str.indexOf(".") > 0) {
			str = str.substring(0, str.length() - 3);
		}

		Long trueValue = Long.parseLong(str) + 1;
		String trueValueStr = trueValue.toString();
		// 对trueValueStr 前面补0；
		int r2 = 4 - (trueValueStr.length() % 4);
		if (r2 < 4 && r2 > 0) {
			for (int i = 0; i < r2; i++) {
				trueValueStr = "0" + trueValueStr;
			}
		}
		return trueValueStr;
	}

	/**
	 * @category 将 OBJ 转换 成 String 防止空串的时候报错
	 */
	public final static String ObjecttoString(Object obj) {
		if (obj == null || obj.equals("")) {
			return " ";
		} else {
			return obj.toString();
		}
	}

	public final static Integer StringToInteger(String str) {
		if (str.equals("null") || str.equals("") || str == null) {
			return 0;
		} else {
			return Integer.parseInt(str);
		}
	}

	/**
	 * @category 编码转换
	 */
	public final static String stringTranscoding(String oldString, String sourseCode, String newCode) {

		String resultString = "";
		try {
			resultString = new String(oldString.getBytes(sourseCode), newCode);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return resultString;
	}
	/**
	 * @category 分割字符串
	 * @param str String 原始字符串
	 * @param splitsign String 分隔符
	 */
	@SuppressWarnings("unchecked")
	public static String[] split(String str, String splitsign) {
		int index;
		if (str == null || splitsign == null) {
			return null;
		}
		ArrayList al = new ArrayList();
		while ((index = str.indexOf(splitsign)) != -1) {
			al.add(str.substring(0, index));
			str = str.substring(index + splitsign.length());
		}
		al.add(str);
		return (String[]) al.toArray(new String[0]);
	}
	/**
	 * @category 替换字符串
	 *
	 * @param from
	 *            String 原始字符串
	 * @param to
	 *            String 目标字符串
	 * @param source
	 *            String 母字符串
	 * @return String 替换后的字符串
	 */
	public static String replace(String from, String to, String source) {
		if (source == null || from == null || to == null)
			return null;
		StringBuffer str = new StringBuffer("");
		int index = -1;
		while ((index = source.indexOf(from)) != -1) {
			str.append(source.substring(0, index) + to);
			source = source.substring(index + from.length());
			index = source.indexOf(from);
		}
		str.append(source);
		return str.toString();
	}

	/**
	 * @category 返回指定字节长度的字符串
	 *
	 * @param str
	 *            String 字符串
	 * @param length
	 *            int 指定长度
	 * @return String 返回的字符串
	 */
	public static String toLength(String str, int length) {
		if (str == null) {
			return null;
		}
		if (length <= 0) {
			return "";
		}
		try {
			if (str.getBytes("GBK").length <= length) {
				return str;
			}
		} catch (Exception e) {
		}
		StringBuffer buff = new StringBuffer();

		int index = 0;
		char c;
		length -= 3;
		while (length > 0) {
			c = str.charAt(index);
			if (c < 128) {
				length--;
			} else {
				length--;
				length--;
			}
			buff.append(c);
			index++;
		}
		buff.append("...");
		return buff.toString();
	}





	/**
	 * @category 判断是不是合法字符 c 要判断的字符
	 */
	public static boolean isLetter(String str) {
		if (str == null || str.length() < 0) {
			return false;
		}
		Pattern pattern = Pattern.compile("[\\w\\.-_]*");
		return pattern.matcher(str).matches();
	}

	/**
	 * @category 提取字符串中的Email地址
	 */
	public static String parse(String content) {
		String email = null;
		if (content == null || content.length() < 1) {
			return email;
		}
		// 找出含有@
		int beginPos;
		int i;
		String token = "@";
		String preHalf = "";
		String sufHalf = "";

		beginPos = content.indexOf(token);
		if (beginPos > -1) {
			// 前项扫描
			String s = null;
			i = beginPos;
			while (i > 0) {
				s = content.substring(i - 1, i);
				if (isLetter(s))
					preHalf = s + preHalf;
				else
					break;
				i--;
			}
			// 后项扫描
			i = beginPos + 1;
			while (i < content.length()) {
				s = content.substring(i, i + 1);
				if (isLetter(s))
					sufHalf = sufHalf + s;
				else
					break;
				i++;
			}
			// 判断合法性
			email = preHalf + "@" + sufHalf;
			if (isEmail(email)) {
				return email;
			}
		}
		return null;
	}

	/**
	 * @category 是否符合Email格式
	 */
	public static boolean isEmail(String email) {
		if (email == null || email.length() < 1 || email.length() > 256) {
			return false;
		}
		Pattern pattern = Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
		return pattern.matcher(email).matches();
	}

	/**
	 * @category 是否符合IP格式
	 */
	public static boolean isIP(String ip) {
		if (ip == null || ip.length() < 1 || ip.length() > 256) {
			return false;
		}
		Pattern pattern = Pattern.compile("^(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])$");
		return pattern.matcher(ip).matches();
	}
	/**
	 * @category 是否是身份证号码格式
	 */
	public static boolean isIdCard(String idCard) {
		if (idCard == null || idCard.length() < 1 || idCard.length() > 256) {
			return false;
		}
		String regIdCard15 = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$";
		String regIdCard18 = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}(\\d|x|X)$";
		if( idCard.trim().length()==15 ){
			Pattern pattern = Pattern.compile(regIdCard15);
			return pattern.matcher(idCard).matches();
		}else if( idCard.trim().length()==18 ){
			Pattern pattern = Pattern.compile(regIdCard18);
			return pattern.matcher(idCard).matches();
		}
		return false;
	}

	/**
	 * @category 是否符合电话格式【手机号码或者座机】
	 */
	public static boolean isTel(String tel) {
		if (tel == null || tel.length() < 1 || tel.length() > 256) {
			return false;
		}
		Pattern pattern = Pattern.compile("(^1[3|4|5|8][0-9]\\d{4,8}$)|^((\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1})|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1}))$");
		return pattern.matcher(tel).matches();
	}

	/**
	 * @category 是否为纯汉字
	 */
	public static boolean isChinese(String str) {
		Pattern pattern = Pattern.compile("[\u0391-\uFFE5]+$");
		return pattern.matcher(str).matches();
	}

	/**
	 * @category 是否为空白,包括null和""
	 */
	public static boolean isBlank(String str) {
		return str == null || str.trim().length() == 0;
	}

	/**
	 * @category 人民币转成大写
	 */
	public static String hangeToBig(String str) {
		double value;
		try {
			value = Double.parseDouble(str.trim());
		} catch (Exception e) {
			return null;
		}
		char[] hunit = { '拾', '佰', '仟' }; // 段内位置表示
		char[] vunit = { '万', '亿' }; // 段名表示
		char[] digit = { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' }; // 数字表示
		long midVal = (long) (value * 100); // 转化成整形
		String valStr = String.valueOf(midVal); // 转化成字符串

		String head = valStr.substring(0, valStr.length() - 2); // 取整数部分
		String rail = valStr.substring(valStr.length() - 2); // 取小数部分

		String prefix = ""; // 整数部分转化的结果
		String suffix = ""; // 小数部分转化的结果
		// 处理小数点后面的数
		if (rail.equals("00")) { // 如果小数部分为0
			suffix = "整";
		} else {
			suffix = digit[rail.charAt(0) - '0'] + "角"
					+ digit[rail.charAt(1) - '0'] + "分"; // 否则把角分转化出来
		}
		// 处理小数点前面的数
		char[] chDig = head.toCharArray(); // 把整数部分转化成字符数组
		char zero = '0'; // 标志'0'表示出现过0
		byte zeroSerNum = 0; // 连续出现0的次数
		for (int i = 0; i < chDig.length; i++) { // 循环处理每个数字
			int idx = (chDig.length - i - 1) % 4; // 取段内位置
			int vidx = (chDig.length - i - 1) / 4; // 取段位置
			if (chDig[i] == '0') { // 如果当前字符是0
				zeroSerNum++; // 连续0次数递增
				if (zero == '0') { // 标志
					zero = digit[0];
				} else if (idx == 0 && vidx > 0 && zeroSerNum < 4) {
					prefix += vunit[vidx - 1];
					zero = '0';
				}
				continue;
			}
			zeroSerNum = 0; // 连续0次数清零
			if (zero != '0') { // 如果标志不为0,则加上,例如万,亿什么的
				prefix += zero;
				zero = '0';
			}
			prefix += digit[chDig[i] - '0']; // 转化该数字表示
			if (idx > 0)
				prefix += hunit[idx - 1];
			if (idx == 0 && vidx > 0) {
				prefix += vunit[vidx - 1]; // 段结束位置应该加上段名如万,亿
			}
		}

		if (prefix.length() > 0)
			prefix += '圆'; // 如果整数部分存在,则有圆的字样
		return prefix + suffix; // 返回正确表示
	}

	/**
	 * @category 去掉字符串中重复的中文字符串
	 */
	public static String removeRepeat(String str){
		if( isBlank(str) ){
			return "";
		}
		char[] charStr = str.toCharArray();
		StringBuffer sb = new StringBuffer();
		List<String> list = new ArrayList<String>();
		//去重
		for( int i=0,ilen=charStr.length; i<ilen; i++ ){
			String tempChar = charStr[i]+"";
			//判断是否为空，集合为空直接添加一条数据
			if( list.isEmpty() ){
				list.add(tempChar);
			}else{
				for( int j=0, jlen=list.size(); j<jlen ; j++ ){
					//如果是标点符号，直接添加一条数据
					if( !isChinese(tempChar) ){
						list.add(tempChar);
						break;
					}else{
						//如果集合中不存在重复则添加一条数据
						if( !list.contains(tempChar) ){
							list.add(tempChar);
							break;
						}
					}
				}
			}
		}
		//转换为字符串
		for( int i=0,ilen=list.size(); i<ilen; i++ ){
			sb.append(list.get(i));
		}
		return sb.toString();
	}


	/**
	 * @category 过滤特殊字符
	 *
	 * @param src
	 * @return
	 */
	public static String encoding(String src) {
		if (src == null)
			return "";
		StringBuilder result = new StringBuilder();
		if (src != null) {
			src = src.trim();
			for (int pos = 0; pos < src.length(); pos++) {
				switch (src.charAt(pos)) {
				case '\"':
					result.append("&quot;");
					break;
				case '<':
					result.append("&lt;");
					break;
				case '>':
					result.append("&gt;");
					break;
				case '\'':
					result.append("&apos;");
					break;
				case '&':
					result.append("&amp;");
					break;
				case '%':
					result.append("&pc;");
					break;
				case '_':
					result.append("&ul;");
					break;
				case '#':
					result.append("&shap;");
					break;
				case '?':
					result.append("&ques;");
					break;
				default:
					result.append(src.charAt(pos));
					break;
				}
			}
		}
		return result.toString();
	}

	/**
	 * @category 判断是不是合法的手机号码
	 *
	 * @param handset
	 * @return boolean
	 */
	public static boolean isHandset(String handset) {
		try {
			String regex = "^1[\\d]{10}$";
			Pattern pattern = Pattern.compile(regex);
			Matcher matcher = pattern.matcher(handset);
			return matcher.matches();

		} catch (RuntimeException e) {
			return false;
		}
	}

	/**
	 * @category 反过滤特殊字符
	 *
	 * @param src
	 * @return
	 */
	public static String decoding(String src) {
		if (src == null)
			return "";
		String result = src;
		result = result.replace("&quot;", "\"").replace("&apos;", "\'");
		result = result.replace("&lt;", "<").replace("&gt;", ">");
		result = result.replace("&amp;", "&");
		result = result.replace("&pc;", "%").replace("&ul", "_");
		result = result.replace("&shap;", "#").replace("&ques", "?");
		return result;
	}

	/**
	 * @category 判断是否为质数
	 * @param x
	 * @return
	 */
	public static boolean isPrime(int x) {
		if (x <= 7) {
			if (x == 2 || x == 3 || x == 5 || x == 7)
				return true;
		}
		int c = 7;
		if (x % 2 == 0)
			return false;
		if (x % 3 == 0)
			return false;
		if (x % 5 == 0)
			return false;
		int end = (int) Math.sqrt(x);
		while (c <= end) {
			if (x % c == 0) {
				return false;
			}
			c += 4;
			if (x % c == 0) {
				return false;
			}
			c += 2;
			if (x % c == 0) {
				return false;
			}
			c += 4;
			if (x % c == 0) {
				return false;
			}
			c += 2;
			if (x % c == 0) {
				return false;
			}
			c += 4;
			if (x % c == 0) {
				return false;
			}
			c += 6;
			if (x % c == 0) {
				return false;
			}
			c += 2;
			if (x % c == 0) {
				return false;
			}
			c += 6;
		}
		return true;
	}

	/**
	 * @category 判断是否为整数
	 *
	 * @param str
	 *            传入的字符串
	 * @return 是整数返回true,否则返回false
	 */
	public static boolean isInteger(String str) {
		Pattern pattern = Pattern.compile("^[-\\+]?[\\d]+$");
		return pattern.matcher(str).matches();
	}

	/**
	 * @category 判断是否为浮点数，包括double和float
	 * @param str
	 *            传入的字符串
	 * @return 是浮点数返回true,否则返回false
	 */
	public static boolean isDouble(String str) {
		Pattern pattern = Pattern.compile("^[-\\+]?\\d+\\.\\d+$");
		return pattern.matcher(str).matches();
	}

	/**
	 * @category 将对象集合转换成字符串数组集合
	 */
	public static <T> List<String[]> objectToArray(List<T> objList){
		List<String[]> strArray = new ArrayList<String[]>();
		for( Object obj : objList ){
			//转换成数组
			String[] attrArray = stringToArray(obj.toString());
			strArray.add(attrArray);
		}
		return strArray;
	}

	/**
	 * @category 将字符串	转换成	字符串数组
	 */
	public static String[] stringToArray(String objString) {
		String[] tempStr = objString.split(",");
		String[] strs;
		String temp="";
		for( int i=0,j=tempStr.length; i<j; i++ ){
			int len = tempStr[i].length();
			int index = tempStr[i].indexOf("=");
			if( i==j-1 )
				temp = temp+tempStr[i].substring(index+1, len-1)+",";
			else
				temp = temp+tempStr[i].substring(index+1, len)+",";
		}
		strs = temp.split(",");
		return strs;
	}

	/**
	 * @category 把首字母转换为大写
	 */
	public static String lastLetterUpper(String fildeName) throws Exception {
		byte[] items = fildeName.getBytes();
		items[0] = (byte) ((char) items[0] - 'a' + 'A');
		return new String(items);
	}
	/**
	 * 判断一个字符串是否不为空或者不是右空白符号组成
	 *
	 * @param source
	 *            String
	 * @return boolean
	 */
	public static boolean notEmpty(String source) {
		return (source != null && source.trim().length() > 0) ? true : false;
	}

	/**
	 * 判断一个字符串是否为空或者是右空白符号组成
	 *
	 * @param source
	 *            String
	 * @return boolean
	 */
	public static boolean empty(String source) {
		return (source == null || source.trim().length() == 0) ? true : false;
	}

	/**
	 * 空字符串转NULL
	 * @param str
	 * @return
	 */
	public static String emptyToNull(String str){
		if( !notEmpty(str) ){
			return null;
		}else{
			return str;
		}
	}

	/**
	 * isContainChinese:(Java 编程下正则表达式判断字符串是否包含中文 )
	 * @author xiaoxie
	 * @param str
	 * @return
	 */
	public static boolean isContainChinese(String str) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }

	/**
	 * @category 获取日期路径字符串
	 * @return
	 */
	public static String datePath(){
		String year = DateFormatUtil.getYear()+"";
		String month = DateFormatUtil.getMonth()+"";
		month=(month.length()==1?"0"+month:month);
		String day = DateFormatUtil.getDay()+"";
		day=(day.length()==1?"0"+day:day);
		return year+"/"+month+day;
	}

	/**
	 * 显示手机号码
	 * @param i 显示前N
	 * @param j 显示后N
	 * @param string 以“*”代替
	 * @param string2 需要修改的字符串
	 * @return
	 */
	public static Object showStringStartAndEnd(int i, int j, String t, String tel) {
		if(tel.length()<=(i+j)){
			//后面的优先显示*
			String f = tel.substring(0, i);
			String m = "";
			for(int h=1;h<=tel.length()-i;h++){
				m += t;
			}
			return f+m;
		}
		String f = tel.substring(0, i);
		String e = tel.substring(tel.length()-j,tel.length());
		String m = "";
		for(int h=1;h<=tel.length()-(i+j);h++){
			m += t;
		}
		return f+m+e;
	}

	public static String getStringRandom(int length) {
		String val = "";
        Random random = new Random();
        //参数length，表示生成几位随机数
        for(int i = 0; i < length; i++) {
        	String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if( "char".equalsIgnoreCase(charOrNum) ) {
				//输出是大写字母还是小写字母
				int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
				val += (char)(random.nextInt(26) + temp);
            } else if( "num".equalsIgnoreCase(charOrNum) ) {
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
	}

	/**
	 * 获取*位随机数(只有数字)
	 * @param length
	 * @return
	 */
	public static String getStringRandomNum(int length) {
		String val = "";
        Random random = new Random();
        for(int i = 0; i < length; i++) {
        	val += String.valueOf(random.nextInt(10));
        }
        return val;
	}


	/**
	 *
	 * @Title: getIntegerListFromString
	 * @Description: 抽取字符串所有连续字数
	 * @category 抽取字符串所有连续字数
	 * @param str
	 * @return    参数
	 * @return List<Integer>    返回类型
	 * @author 颜子健
	 * @date 2017年5月6日
	 * @throws
	 */
	public static List<Integer> getIntegerListFromString(String str){
		List<Integer> list = new ArrayList<>();
		if(!StringUtil.empty(str)){
			Pattern pattern = Pattern.compile("\\d+");
			Matcher matcher = pattern.matcher(str);
			while (matcher.find()) {
	           list.add(Integer.parseInt(matcher.group(0)));
			}
		}
		return list;
	}

	/**
	 *
	 * @Title: joinForCollection
	 * @Description: 将字符串集合按指定字符连接返回
	 * @param values
	 * @param string
	 * @return    参数
	 * @return String    返回类型
	 * @author x3455
	 * @date 2017年5月6日
	 * @throws
	 */
	public static String joinForCollection(Collection<String> values, String string) {
		StringBuffer re = new StringBuffer();
		for (String val : values) {
			re.append(val);
			re.append(string);
		}
		//删除最后一个字符
		if(re.length()>1){
			re.deleteCharAt(re.length()-1);
		}
		return re.toString();
	}

	/**
	 *
	 * @Title: getNullPropertyNames
	 * @Description: 返回为空值的属性名称数组
	 * @category：返回为空值的属性名称数组
	 * @param source
	 * @return    参数
	 * @return String[]    返回类型
	 * @author x3455
	 * @date 2017年5月6日
	 * @throws
	 */
	public static String[] getNullPropertyNames (Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNames = new HashSet<String>();
        for(java.beans.PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) emptyNames.add(pd.getName());
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }
	/**
     * getPK,获得数据库使用的一个long型唯一主键
     * 16位，同一微秒内3000个不会重复
     * @return long
     */
    public synchronized static long getPK(){
        long lo = getpk();
        for (int i = 0; i < 3000; i++)
        {
            long lt = ls[i];
            if (lt == lo)
            {
                lo = getPK();
                break;
            }
        }
        ls[li] = lo;
        li++;
        if (li == 3000)
        {
            li = 0;
        }
        return lo;
    }

    private static long getpk(){
        String a = (String.valueOf(System.currentTimeMillis())).substring(3, 13);
        String d = (String.valueOf(Math.random())).substring(2, 8);
        return Long.parseLong(a + d);
    }

	/**
	 * 从指定字符串中随机获取length个字符进行拼接
	 * @param length
	 * @return
	 */
	public static String getRandomNumber(int length) {
	    StringBuffer buffer = new StringBuffer("0123456789");
	    StringBuffer sb = new StringBuffer();
	    Random r = new Random();
	    int range = buffer.length();
	    while(true){
		    for (int i = 0; i < length; i ++) {
		        sb.append(buffer.charAt(r.nextInt(range)));
		    }
		    if(sb.substring(0, 1).equals("0"))sb.delete(0, sb.length());
		    if(sb.length()==length)break;
	    }
	    return sb.toString();
	}
	/**
	 * 当数值类型
	 * 小数点后为0时,则取整数,否则保留小数点后的2位数,返回String
	 * @param obj
	 * @return
	 */
	public static String formatNumberToString(Object obj) {
		if (obj == null) { return "0";}
		DecimalFormat df1 = new DecimalFormat("0.00");
		String str = df1.format(new BigDecimal(String.valueOf(obj)));
		String plainString = new BigDecimal(String.valueOf(str)).stripTrailingZeros().toPlainString();
		return plainString;
	}

	/**
	 * 判断手机号码
	 * @param tel
	 * @return 前三后四带*号：0 开头为1的11为数字：1 其他：2
	 */
	public static int regexpTel(String tel) {
		if(StringUtil.empty(tel)) {
			return 2;
		}
		//带*
		String x = "[1][0-9]{2}[*]{4}[0-9]{4}";
		//数字
		String s = "[1][0-9]{2}[0-9]{8}";
		Pattern pattern = Pattern.compile(x);
		Matcher matcher = pattern.matcher(tel);
		Boolean flag = matcher.matches();
		if(flag) {
			return 0;
		}
		pattern = Pattern.compile(s);
		matcher = pattern.matcher(tel);
		flag = matcher.matches();
		if(flag) {
			return 1;
		}
		return 2;
	}
}

